package com.woniu40.petai_user.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu40.petai_user.entity.ArtType;
import com.woniu40.petai_user.entity.Article;
import com.woniu40.petai_user.entity.Collect;
import com.woniu40.petai_user.mapper.ArtTypeMapper;
import com.woniu40.petai_user.mapper.ArticleMapper;
import com.woniu40.petai_user.mapper.CollectMapper;
import com.woniu40.petai_user.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;
import java.util.List;

@Service
@EnableTransactionManagement
public class ArticleService {

    @Autowired
    ArticleMapper articleMapper ;
    @Autowired
    ArtTypeMapper artTypeMapper ;
    @Autowired
    CollectMapper collectMapper ;

    //查询所有分类
    public List<ArtType> findArtType() {
        return artTypeMapper.selectAll();
    }
//    用户添加收藏
    public int addNewCollectInfo(Integer userId,Integer articleId,Date dataTime) {
        collectMapper.insertNewCollectInfo(userId,articleId,dataTime);
        articleMapper.updateAddCol(articleId);
        return 1;
    }
    //用户删除收藏
    public int removeNewCollectInfo(Integer collectId,Integer articleId) {
         collectMapper.deleteCol(collectId);
         articleMapper.updateSubCol(articleId);
        return 1;
    }
//    模糊搜索
    public PageInfo<Article> findByInput(Integer currentPage, Integer pageSize, String articleContent, Integer artTypeId,Integer type){
        PageHelper.startPage(currentPage,pageSize);
        return new PageInfo<>(articleMapper.selectByInput(articleContent,artTypeId,type));
    }

    //获取精选
    public List<Article> findArtTop() {
        return articleMapper.selectArtTop();
    }
    //阅读增加浏览量
    public void updateView(Integer articleId){
        articleMapper.updateView(articleId);
    }

    public Article findArtById(Integer articleId){
        return articleMapper.selectArtById(articleId);
    }

    public List<ArticleVo> findByCreateTime(){
        List<ArticleVo> articles = articleMapper.selectByCreateTime();
        return articles;
    }

    public Collect findcol(Integer articleId, Integer userId) {
        return collectMapper.selectCol(articleId,userId);
    }
}
